package com.sx.pipeline.ui.mine

import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.callback.NavCallback
import com.blankj.utilcode.util.ActivityUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.hjq.bar.TitleBar
import com.lxj.xpopup.XPopup
import com.sx.mvvmlibs.utils.Logs
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYBaseAppBarActivity
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActSwitchBusinessBinding
import com.sx.pipeline.repos.http.res.IdentityVO
import com.sx.pipeline.repos.http.res.MyUnitListVO
import com.sx.pipeline.utils.CacheDataManager

/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 切换企业
 */
@Route(path = RoutePath.Main.SWITCH_BUSINESS)
class SwitchBusinessActivity : CYBaseAppBarActivity<ActSwitchBusinessBinding, SwitchBusinessVM>(
    R.layout.act_switch_business) {

    private lateinit var businessAdapter: SwitchBusinessAdapter

    override fun initAppBar() {
        getTitleBar().title = getString(R.string.switch_business)
        getTitleBar().rightTitle = getString(R.string.txt_add_business)
        getTitleBar().setRightTitleColor(Color.parseColor("#0188FF"))
    }


    override fun initData() {
        mViewModel.getSwitchUnitList()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 可切换单位列表
        setSwitchUnitList()
    }

    private fun setSwitchUnitList() {
        observe(mViewModel.switchUnitListEvent) {
            if (!this::businessAdapter.isInitialized) {
                mBinding.recyclerUnit.layoutManager = LinearLayoutManager(
                    this, LinearLayoutManager.VERTICAL, false
                )
                businessAdapter = SwitchBusinessAdapter(it as MutableList<MyUnitListVO>)
                mBinding.recyclerUnit.adapter = businessAdapter
                businessAdapter.setOnNestChildClickListener(object : SwitchBusinessAdapter.OnNestChildClickListener {
                    override fun onItemClickListener(
                        item: MyUnitListVO,
                        parentPosition: Int,
                        childPosition: Int
                    ) {
                        Logs.d("setOnNestChildClickListener $parentPosition == $childPosition == ${item.unitId}")
                        if (item.identity[childPosition].isEnabled) {
                            show(item.unitId, item.identity[childPosition].identity)
                        }
                    }
                })

                businessAdapter.addChildClickViewIds(R.id.image_select)
                businessAdapter.setOnItemChildClickListener { adapter, view, position ->
                    Logs.d("setOnItemChildClickListener")

                }
            }
        }
    }

    private fun show(unitId: Int, identity: String) {
        XPopup.Builder(this)
            .isDestroyOnDismiss(true)
            .asConfirm(
                getString(R.string.hint), getString(R.string.q_switch_business),
                getString(R.string.cancel), getString(R.string.confirm),
                {
                    mViewModel.switchUnitIdentity(identity, unitId)
                }, {
                    businessAdapter.removeSelection()
                   }, false
            ).show()
    }

    override fun onRightClick(titleBar: TitleBar?) {
        RouteManager.Main.intentToJoinBusiness()
    }
}